/**
 * 数据校验
 */
package aorey.database;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

public class DataCheck
{

    protected String config;

    public String error = null;

    public HashMap<String, DataType> dType;

    /**
     * 序列化配置信息
     *
     * @param config
     * @return
     * @throws DocumentException
     */
    public HashMap<String, DataType> getDataType(String config) throws DocumentException
    {
        File configFile = new File(config);
        if (configFile.exists())
        {
            HashMap<String, DataType> result = new HashMap<>();
            SAXReader reader = new SAXReader();
            Document document = reader.read(configFile);
            Element node = document.getRootElement();
            List<Element> item = node.elements();
            int listSize = item.size();
            for (int i = 0; i < listSize; i++)
            {
                DataType dataType = new DataType();
                String name = item.get(i).attribute("name").getData().toString();
                dataType.reg = item.get(i).attribute("reg").getData().toString();
                dataType.input = item.get(i).attributeValue("input");
                result.put(name, dataType);
            }
            this.dType = result;
            return result;
        } else
        {
        	System.out.println("dataType配置文件错误!");
            return null;
        }
    }

    /**
     * 获取页面值
     *
     * @param type get/post/session/cookie/application
     * @param name
     * @param server
     * @return
     */
    public String getFormValue(HttpServletRequest request, String type, String name)
    {
        String value = "";
        switch (type)
        {
            case "get":
            case "post":
            {
                if (request.getParameter(name) != null)
                {
                    value = request.getParameter(name);
                }
                break;
            }
            case "session":
            {
                if (request.getSession().getAttribute(name) != null)
                {
                    value = request.getSession().getAttribute(name).toString();
                }
                break;
            }
            case "application":
            {
                if (request.getServletContext().getAttribute(name) != "")
                {
                    value = request.getServletContext().getAttribute(name).toString();
                }
                break;
            }
            case "cookie":
            {
                Cookie[] cookies = request.getCookies();
                int cookie_len = cookies.length;
                for (int i = 0; i < cookie_len; i++)
                {
                    Cookie cookie = cookies[i];
                    if (cookie.getName().equals(name) && cookie.getValue() != null)
                    {
                        value = cookie.getValue();
                    }
                }
            }
            default:
            {

            }
        }
        return value;
    }

    /**
     * 校验数据
     *
     * @param value 校验值
     * @param type 数据类型
     * @param allowNull 是否允许为空, true 允许为空, false 不允许为空
     * @param len 长度
     * @return
     */
    public boolean checkValue(String value, String type, String allowNull, int len)
    {
        if ((allowNull.equals("no") && (value == null || value == "")) || (len > 0 && value.length() > len) || this.checkType(value, type) == false)
        {
            return false;
        }
        return true;
    }

    /**
     * 校验数据 类型是否匹配
     *
     * @param value 需要校验的数值
     * @param type 数据类型
     * @return
     */
    public boolean checkType(String value, String type)
    {
        DataType dataType = this.dType.get(type);
//        System.out.println(this.dType.get(type).reg);
        if (dataType != null)
        {
//        	System.out.println("reg error!:"+value);
            return Pattern.matches(dataType.reg, value);
        } else
        {
            return false;
        }
    }

    public void setDType(HashMap<String, DataType> dType)
    {
        this.dType = dType;
    }
}
